#ifndef DISPLAY_H
#define DISPLAY_H

#include <esp_log.h>
#include <esp_pm.h>
#include <esp_timer.h>
#include <lvgl.h>

#include <string>

#include "screenMain.h"

class Display {
   public:
    Display();
    virtual ~Display();

    virtual void InitUI();
    virtual void SetStatus(const char* status);
    virtual void ShowNotification(const char* notification, int duration_ms = 3000);
    virtual void ShowNotification(const std::string& notification, int duration_ms = 3000);
    virtual void SetEmotion(const char* emotion);
    virtual void SetChatMessage(const char* role, const char* content);
    virtual void SetIcon(const char* icon);
    virtual void SetTheme(const std::string& theme_name);
    virtual void setMainPanel(screen_main_state_t state);
    virtual std::string GetTheme() { return theme_; }

    inline int width() const { return width_; }
    inline int height() const { return height_; }

   protected:
    lv_display_t* display_ = nullptr;
    int width_ = 0;
    int height_ = 0;
    std::string theme_;

    screen_main_state_t state_ = SCREEN_MAIN_STATE_LOADING;
    bool bat_anime = false;
    
    esp_pm_lock_handle_t pm_lock_ = nullptr;
    esp_timer_handle_t notification_timer_ = nullptr;
    esp_timer_handle_t update_timer_ = nullptr;
    
    friend class DisplayLockGuard;
    virtual bool Lock(int timeout_ms = 0) = 0;
    virtual void Unlock() = 0;

    virtual void Update();
};

class DisplayLockGuard {
   public:
    DisplayLockGuard(Display* display) : display_(display) {
        if (!display_->Lock(30000)) {
            ESP_LOGE("Display", "Failed to lock display");
        }
    }
    ~DisplayLockGuard() { display_->Unlock(); }

   private:
    Display* display_;
};

class NoDisplay : public Display {
   private:
    virtual bool Lock(int timeout_ms = 0) override { return true; }
    virtual void Unlock() override {}
};

#endif
